var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
	conn.createChannel(function(err, ch) {
		var q = 'task_queue';
		ch.assertQueue(q, {
			durable: true
		});
		ch.prefetch(1);

		console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
		ch.consume(q, function(msg) {
			var msgContent = msg.content.toString();
			console.log(" [x] Received %s", msgContent);
			var delay = Math.floor(Math.random() * 10 + 1); // 1~10 sec
			setTimeout(function() {
				console.log(" [x] Done %s", msgContent);
				ch.ack(msg);
			}, delay * 1000);
		}, {
			noAck: false
		});
	});
});